package com.n9.qq.android.login;

import com.n9.qq.util.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.ByteBuffer;

/**
 * @author ice
 * @since 2019/2/19 14:31
 */
public abstract class BaseReqLoginBody {

    private short version = 0x1F41;
    private short cmd = 0x0810;
    protected byte unKnowData = 0x07;
    protected long qqUin;

    public byte[] getData(byte[] clientPubKey, byte[] shareKey, byte[] randomKey) {
        byte[] enData = encryptData(shareKey);

        final int len = 4 + 9 + 4 + 17 + 16 + 4 + clientPubKey.length + enData.length + 1;
        ByteBuffer buf = ByteBuffer.allocate(len);
        buf.putInt(len);
        buf.put((byte)2);
        buf.putShort((short)(len - 4));
        buf.putShort(version);
        buf.putShort(cmd);
        buf.putShort((short)0001);
        byte[] qqUinBytes = Util.int64ToBuf32(qqUin);
        buf.put(qqUinBytes);

        buf.put((byte)3);
        buf.put(unKnowData);
        buf.put((byte)0);

        buf.putInt(2);
        buf.putInt(0);
        buf.putInt(0);

        buf.put((byte)1);
        buf.put((byte)1);

        buf.put(randomKey);

        buf.putShort((short)0x0102);
        buf.putShort((short)clientPubKey.length);
        buf.put(clientPubKey);

        buf.put(enData);
        buf.put((byte)03);

        return buf.array();
    }

    protected abstract byte[] encryptData(byte[] shareKey);
}
